$(document).ready(function() {
	customerListSupport();
	getAccountInfos();
	loadTab();
});
function updateChecked(status) {
	$("input[name=checkbox]").each(function() {
		this.checked = status;
	});
}
function checkAll() {
	updateChecked($("#checkAll").is(':checked'));
}
function updateAccountInfo() {
	$.ajax({
		type : "POST",
		url : "json/accountinfo/updateAccountInfo",
		data : {
			"accountIdEdit" : $('#accountIdEdit').val(),
			"accountNumberEdit" : $('#accountNumberEdit').val(),
			"identityCardEdit" : $('#identityCardEdit').val(),
			"firstNameEdit" : $('#firstNameEdit').val(),
			"midNameEdit" : $('#midNameEdit').val(),
			"lastNameEdit" : $('#lastNameEdit').val(),
			"phone1Edit" : $('#phone1Edit').val(),
			"phone2Edit" : $('#phone2Edit').val(),
			"email1Edit" : $('#email1Edit').val(),
			"email2Edit" : $('#email2Edit').val(),
			"stateEdit" : $('#stateEdit').val()
		},
		dataType : 'json',
		success : function(response) {
			if (response == true) {
				getAccountInfos();
				$('#md_transdetail2').modal('toggle');
			}
		},
		error : function(e) {
		}
	});
}
function getAccountDetail(id) {
	$.ajax({
		type : "GET",
		url : "json/accountinfo/getAccountById/" + id,
		data : null,
		dataType : 'json',
		success : function(response) {
			$('#cusId').html(response.accountId);
			$('#amount').html(response.accountNumber);
			$('#identityCard').html(response.identityCard);
			$('#cusName').html(response.firstName);
			$('#midName').html(response.midName);
			$('#lastName').html(response.lastName);
			$('#phone1').html(response.phone1);
			$('#phone2').html(response.phone2);
			$('#email1').html(response.email1);
			$('#email2').html(response.email2);
			$('#state').html(response.state);
			$('#state').html(response.stateName);
		},
		error : function(e) {
			// alert('Error: ' + e);
		}
	});
}
function getAccountEdit(idEdit) {
	$.ajax({
		type : "GET",
		url : "json/accountinfo/getAccountById/" + idEdit,
		data : null,
		dataType : 'json',
		success : function(response) {
			$('#accountIdEdit').val(response.accountId);
			$('#accountNumberEdit').val(response.accountNumber);
			$('#identityCardEdit').val(response.identityCard);
			$('#firstNameEdit').val(response.firstName);
			$('#midNameEdit').val(response.midName);
			$('#lastNameEdit').val(response.lastName);
			$('#phone1Edit').val(response.phone1);
			$('#phone2Edit').val(response.phone2);
			$('#email1Edit').val(response.email1);
			$('#email2Edit').val(response.email2);
			$('#stateEdit').val(response.state);
			$('#stateEdit').val(response.state);
			if (response.state == 2) {
				var content = "<button type='button' class='btn btn-success' onclick='changeStateButton();' id='btnStateChange' style='font-size:16px;width:150px'>Active</button>";
				$('#stateContentButton').html(content);
			} else {
				
				var content = "<span class='label label-success' style='font-size:16px' id='btnStateChange'>"+response.stateName+"</span>";
				$('#stateContentButton').html(content);
				
			}
		},
		error : function(e) {
			alert('Error: ' + e);
		} // chay lai thu :D
	});
}
function getAccountInfos() {
	$
			.ajax({
				type : "POST",
				url : "json/accountinfo/getAllAccount",
				data : null,
				dataType : 'json',
				contentType : "application/json",

				success : function(response) {
					var content = "";
					$
							.each(
									response,
									function(i, post) {
										content += "<tr role='row' class='odd'>";
										content += "<td class='sorting_1'>";
										content += post.accountNumber;
										content += "</td>";

										content += "<td>";
										content += post.identityCard;
										content += "</td>";

										content += "<td>";
										content += post.firstName;
										content += "</td>";

										content += "<td>";
										content += post.lastName;
										content += "</td>";

										content += "<td>";
										content += post.phone1;
										content += "</td>";

										content += "<td>";
										content += post.email1;
										content += "</td>";

										content += "<td style=\"text-align:center\">";
										content += post.stateName;
										content += "</td>";

										content += "<td style=\"text-align:center\"><button type=\"button\"";
										content += "onclick=\"getAccountDetail("
												+ post.accountIdEdit + ")\"";
										content += "class=\"btn btn-default\" data-toggle=\"modal\"";
										content += "data-target=\"#md_transdetail\"";
										content += "value=\"\">Detail</button></td>";
										content += "<td style=\"text-align:center\"><button type=\"button\" class=\"btn btn-default\"";
										content += "onclick=\"getAccountEdit("
												+ post.accountIdEdit + ")\"";
										content += "data-toggle=\"modal\" data-target=\"#md_transdetail2\">Edit</button></td>";

										content += "</tr>";

									});
					$('#tbodyAccount').html(content);
				},
				error : function(xhr, ajaxOptions, thrownError) {
					alert('Error2: dasdsad ');
				}
			});

}
function loadTab() {
	$
			.ajax({
				type : "POST",
				url : "json/accountinfo/getAccountsByState",
				data : {
					"stateId" : 2
				},
				dataType : 'json',
				success : function(response) {
					var content = "";
					$("#checkAllArea3")
							.html(
									"<input type=\"checkbox\" style=\"text-align:left\" onchange=\"checkAll()\" id='checkAll'/>");
					$.each(response, function(i, post) {

						content += "<tr role='row' class='odd'>";
						content += "<td style=\"text-align:left\"><input type='checkbox' value='"
								+ post.accountId
								+ "' name='checkbox' class='checkbox'/></td>";
						content += "<td class='sorting_1'>";
						content += post.accountNumber;
						content += "</td>";

						content += "<td>";
						content += post.firstName;
						content += "</td>";

						content += "<td>";
						content += post.lastName;
						content += "</td>";

						content += "<td>";
						content += post.state;
						content += "</td>";

						content += "</tr>";

					});
					$("#tbodyActiveTab").html(content);
					$('#myTab a:first').tab('show');
				},
				error : function(e) {
				}
			});
}

function transferState(stateId) {
	var matches = [];
	$(".checkbox:checked").each(function() {
		matches.push(this.value);
	});

	jQuery.each(matches, function(index, value) {
		$.ajax({
			type : "POST",
			url : "json/accountinfo/transferState",
			data : {
				"accountId" : value,
				"stateId" : stateId
			},
			dataType : 'json',
			success : function(response) {
				if (response == true) {
					loadTab();
				}
			},
			error : function(e) {
				alert('Error: transferState');
			}
		});
	});
}
function customerListSupport() {
	$.ajax({
		type : "GET",
		url : "tableDetailSupport",
		success : function(response) {
			$('.main-content').html(response);
			getAccountInfos();
		},
		error : function(e) {
		}
	});
}
function manageStateSupport() {
	$.ajax({
		type : "GET",
		url : "tableStateSupport",
		success : function(response) {
			$('.main-content').html(response);
			loadTab();
		},
		error : function(e) {
		}
	});
}
function addAccount() {
	$.ajax({
		type : "GET",
		url : "addAccount",
		success : function(response) {
			$('.main-content').html(response);
			$('#sentErr').hide();
		},
		error : function(e) {
		}
	});
}
function addNewAccount()
{
	$.ajax({
		type : "POST",
		url : "json/accountinfo/addNewAccount",
		data : {
			"accountNumber" : $('#accountNumberAdd').val(),
			"identityCard" : $('#identityCardAdd').val(),
			"firstName" : $('#firstNameAdd').val(),
			"midName" : $('#midNameAdd').val(),
			"lastName" : $('#lastNameAdd').val(),
			"phone1" : $('#phone1Add').val(),
			"phone2" : $('#phone2Add').val(),
			"email1" : $('#email1Add').val(),
			"email2" : $('#email2Add').val()
		},
		dataType : 'json',
		success : function(response) {
			if (response == true) {
				$('#accountDetail').click();
			}
		},
		error : function(e) {
		}
	});
}
function cancelAccount()
{
	$('#accountDetail').click();
}
function validate()
{
   if( $('#accountNumberAdd').val() == "" )
   {
	   $("#sentErr").html("please enter your account number!!!");
    
   }else if( $('#identityCardAdd').val() == "" )
   {
	   alert( "Please enter identity card" );
	    
   }else if( $('#firstNameAdd').val() == "" )
   {
	   alert( "Please enter firstName" );
	    
   }else if( $('#lastNameAdd').val() == "" )
   {
	   alert( "Please enter lastNameAdd" );
	    
	}else if( $('#phone1Add').val() == "" )
	{
		alert( "Please enter phone 1" );
	   
	}else if( $('#email1Add').val() == "" )
	{
	   alert( "Please enter email1" );
	   
	}else if(IsEmail($('#email1Add').val())==false){
               alert("email not right");
    }
	else if(IsNumeric($('#phone1Add').val())==false){
        alert("phone not right");
	}
	else if(IsNumeric($('#identityCardAdd').val())==false){
        alert("identity not right");
	}
	else if(IsNumeric($('#accountNumberAdd').val())==false){
        alert("account number not right");
	}
	else{
		addNewAccount();
	}
   $('#sentErr').show();

}
function IsEmail(email) {
    var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(!regex.test(email)) {
       return false;
    }else{
       return true;
    }
 }
function IsNumeric(input){
    var RE = /^-{0,1}\d*\.{0,1}\d+$/;
    if(!RE.test(input)) {
        return false;
     }else{
        return true;
     }
}
function changeStateButton() {
	if ($('#btnStateChange').html() == "Active") {
		$('#btnStateChange').html('Mark As Disable');
		$('#stateEdit').val(3);
	} else {
		$('#btnStateChange').html('Active');
		$('#stateEdit').val(2);
		
	}
}